package cn.damong.service.response;

/**
 * Service层向调用层返回的结果，包含返回状态、提示语与Service结果
 * 
 * @author CodingPanda
 * @2019年5月15日 上午12:02:05
 *
 */
public class ServiceResponse {
    /**
     * 0表示失败，1表示成功，其他值按照约定, 默认0
     */
	private Integer status = 0; 
	
	/**
	 * Service接口的提示信息
	 */
	private String message = "";
	
	/**
	 * Service接口返回的数据
	 */
	private Object data = null;
	
	public ServiceResponse() {
		
	}
	
	public ServiceResponse(Integer status, String message) {
        this.status = status;
        this.message = message;
    }
	
	public ServiceResponse(Integer status, String message, Object data) {
		this.status = status;
		this.message = message;
		this.data = data;
	}
	
	/**
	 * 接口调用成功，无返回值
	 * 
	 * @return
	 */
	public static ServiceResponse success() {
        ServiceResponse result = new ServiceResponse();
        result.status = 1;
        return result;
    }
	
	/**
	 * 接口调用成功，返回值为data
	 * 
	 * @param data
	 * @return
	 */
	public static ServiceResponse success(Object data) {
		ServiceResponse result = new ServiceResponse();
		result.status = 1;
		result.message = "";
		result.data = data;
		return result;
	}
	
	/**
	 * 接口调用失败，返回错误信息
	 * 
	 * @param message
	 * @return
	 */
	public static ServiceResponse fail(String message) {
		ServiceResponse result = new ServiceResponse();
		result.message = message;
		result.data = null;
		return result;
	}
	
	/**
	 * 接口调用是否成功
	 * 
	 * @return
	 */
	public boolean isSuccess() {
        return this.status == 1;
    }

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((data == null) ? 0 : data.hashCode());
		result = prime * result + ((message == null) ? 0 : message.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
            return true;
        }
		if (obj == null) {
            return false;
        }
		if (getClass() != obj.getClass()) {
            return false;
        }
		ServiceResponse other = (ServiceResponse) obj;
		if (data == null) {
			if (other.data != null) {
                return false;
            }
		} else if (!data.equals(other.data)) {
            return false;
        }
		if (message == null) {
			if (other.message != null) {
                return false;
            }
		} else if (!message.equals(other.message)) {
            return false;
        }
		if (status == null) {
			if (other.status != null) {
                return false;
            }
		} else if (!status.equals(other.status)) {
            return false;
        }
		return true;
	}

	@Override
	public String toString() {
		return "ServiceResponse [status=" + status + ", message=" + message + ", data=" + data + "]";
	}

}
